<template>
  <!-- 素材包管理 -->
  <div class="material-pack-list">
    <top-tab :list="tabList" :tabIndex.sync="tabIndex"/>
    <div class="content">
      <p class="title">{{ tabName }}素材包：<span class="green-text">{{ list.length }}个</span></p>
      <div class="list">
        <div class="item" 
          @click="itemClick(item)"
          v-for="(item, index) in list" 
          :key="index">
          <div 
            class="img-wrapper" 
            :style="{backgroundImage: 'url(' + item.cover_url + ')'}">
          </div>
          <p class="name">{{ item.package_name }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import topTab from '@/components/top-tab.vue'
import { listPackageByTopic } from '@/api/account'
import { getOssDomain } from '@/api/oss'

export default {
  components: { topTab },
  data () {
    return {
      list: [],
      tabList: ['产品', '人物', '场景'],
      tabIndex: 0,
      typeDict: {
        '产品': 'GOODS',
        '人物': 'PEOPLE',
        '场景': 'LOCATION'
      },
      oss_domain: '',
    }
  },
  watch: {
    tabIndex (val) {
      this.getListByType()
    }
  },
  computed: {
    role () {
      return sessionStorage.getItem('role')
    },
    tabName () {
      return this.tabList[this.tabIndex]
    }
  },
  async mounted () {
    if (!sessionStorage.getItem('oss_domain')) {
      const res = await getOssDomain()
      sessionStorage.setItem('oss_domain', res.domain)
      this.oss_domain = res.domain
    } else {
      this.oss_domain = sessionStorage.getItem('oss_domain')
    }
    this.getListByType()
    document.title = '素材包管理'
  },
  methods: {
    getListByType () {
      listPackageByTopic({
        topic: this.typeDict[this.tabList[this.tabIndex * 1]]
      }).then(res => {
        console.log('素材包列表：', res)
        res.forEach(item => {
          if (item.cover_url) {
            item.cover_url = `${this.oss_domain}/${item.cover_url}`
          }
        })
        this.list = res
      })
    },
    itemClick (item) {
      this.$router.push({
        path: '/materialPackDetail',
        query: {
          package_id: item.id,
          type: this.tabList[this.tabIndex * 1],
          package_name: item.package_name
        }
      })
    }
  }
}
</script>

<style scoped>
.material-pack-list {
  padding-top: 21.33vw;
  padding-bottom: 21.33vw;

}
.list {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.item {
  margin-bottom: 4.27vw;
}
.img-wrapper {
  width: 36vw;
  height: 53.33vw;
  margin-bottom: 2.13vw;
  background-color: #000000;
  background-size: contain;
  background-repeat: no-repeat;
  background-position: center;
  border-radius: 2.13vw;
}
.name {
  width: 36vw;
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  line-height: 22px;
  color: #666666;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.content {
  box-sizing: border-box;
  width: 89.6vw;
  box-sizing: border-box;
  padding: 3.73vw 5.33vw 0;
  background: #ffffff;
  margin: 0 auto;
  border-radius: 2.13vw;
}
.title {
  margin-bottom: 5.87vw;
  font-family: Source Sans Pro;
  font-size: 3.73vw;
  font-weight: 600;
  line-height: 5.87vw;
  color: #3D3D3D;
}
.green-text {
  color: #00A5A6;
}
</style>